package com.woniuxy.homepage.core.service.impl;

import com.woniuxy.common.CommonResult;
import com.woniuxy.homepage.client.PersonalServiceFeignClient;
import com.woniuxy.homepage.client.dto.OrderDto;
import com.woniuxy.homepage.client.dto.OrderitemDto;
import com.woniuxy.homepage.core.constant.PromotionConstant;
import com.woniuxy.homepage.core.exception.SeckillException;
import com.woniuxy.homepage.core.model.CarGoodsActivity;
import com.woniuxy.homepage.core.mapper.CarGoodsActivityMapper;
import com.woniuxy.homepage.core.service.CarGoodsActivityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.homepage.core.vo.CarGoodsActivityVo;
import io.swagger.models.auth.In;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gmx
 * @since 2021-07-03
 */
@Service
public class CarGoodsActivityServiceImpl extends ServiceImpl<CarGoodsActivityMapper, CarGoodsActivity> implements CarGoodsActivityService {

    @Resource
    private CarGoodsActivityMapper mapper;

    @Resource
    private PersonalServiceFeignClient client;

    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public List<CarGoodsActivityVo> selectAll(int count) {
        List<CarGoodsActivityVo> list = mapper.selectAll(count);
        return list;
    }

    @Override
    public int buyActivityGoods(Integer goodsId,Integer userId) {


        //判断该用户是否已经够购买过此商品
        boolean flag = redisTemplate.opsForSet().isMember("activity:user:"+goodsId,userId);
        System.out.println("调用接口"+flag);
        if (!flag){
            //根据商品id 查询参与的活动
            CarGoodsActivity carGoodsActivity = this.mapper.selectByGoodsId(goodsId);
            Integer i = (Integer) redisTemplate.opsForList().leftPop("activity:count:"+carGoodsActivity.getId());
            if (carGoodsActivity == null){
                throw new SeckillException(500,"商品不存在");
            }
            if (carGoodsActivity.getStatus().equals(PromotionConstant.UN_START_STATUS)){
                throw new SeckillException(500,"活动未开始");
            }
            if (carGoodsActivity.getStatus().equals(PromotionConstant.OVER_STATUS)){
                throw new SeckillException(500,"活动已结束");
            }
            if (i==null){
                throw new SeckillException(500,"商品已经售罄");
            }
            //判断该用户是否已经购买过该商品
            //可以抢购，生成订单
            OrderDto orderDto = new OrderDto();

            OrderitemDto orderitemDto = new OrderitemDto();
            orderitemDto.setGoodsId(goodsId);
            orderitemDto.setQuantity(1);
            List<OrderitemDto> list = new ArrayList<>();
            list.add(orderitemDto);

            orderDto.setOrdertype(0);
            orderDto.setUserId(userId);
            orderDto.setOrderitemVos(list);

            System.out.println(orderDto);
            CommonResult order = this.client.createOrder(orderDto);

            if (order.getCode() == 200){
                System.out.println("成功生成订单");
                //成功生成订单,讲用户储存在redis中
                redisTemplate.opsForSet().add("activity:user:"+goodsId,userId);
                //更改数据库活动商品的数量

            }else {
                throw new SeckillException(500,"订单创建失败");
            }
            return 200;
        }else {
            throw new SeckillException(500,"商品不能重复抢购");
        }
    }
}
